﻿using NetCore8Demo.Tool;
using Newtonsoft.Json;

namespace NetCore8Demo.Models.PLC;

public class StaticPlc
{

    public static string SavePath = "Communication";
    public static string AddressInfoSaveName = "PlcAddressInfoList";
    public static string PlcInfoSaveName = "PlcInfoList";


    public static List<PlcAddressInfo> PlcAddressInfoList = new();
    public static List<PlcInfo> PlcInfoList = new();


    public static void InitStaticPlcAddressInfoList()
    {
        PlcInfoList = JsonParam.LoadParamPath<List<PlcInfo>>(SavePath, PlcInfoSaveName) ??
                      new List<PlcInfo>();

        PlcAddressInfoList = JsonParam.LoadParamPath<List<PlcAddressInfo>>(SavePath, AddressInfoSaveName) ??
                             new List<PlcAddressInfo>();
       
    }

    public static List<PlcInfo> LoadPlcInfoList()
    {
        List<PlcInfo> plcInfoList =
            JsonParam.LoadParamPath<List<PlcInfo>>(SavePath, PlcInfoSaveName) ?? new List<PlcInfo>();
        return plcInfoList;
    }

    public static bool SavaPlcInfoList(List<PlcInfo> plcInfoList)
    {
        return JsonParam.SaveParamPath(SavePath, PlcInfoSaveName, plcInfoList);
    }

    public static List<PlcAddressInfo> LoadPlcAddressInfoList()
    {
        List<PlcAddressInfo> plcAddressInfoList = 
            JsonParam.LoadParamPath<List<PlcAddressInfo>>(SavePath, AddressInfoSaveName) ?? new List<PlcAddressInfo>();
        return plcAddressInfoList;
    }

    public static bool SavaPlcAddressInfoList(List<PlcAddressInfo> plcAddressInfoList)
    {
        return JsonParam.SaveParamPath(SavePath, AddressInfoSaveName, plcAddressInfoList);
    }

   


}

public class PlcInfo
{
    public Guid Id;
    public int Index;
    public string Name = "";
    public string Ip = "";
    public int Port = 0;
    public PlcType PlcType;
    public bool IsInUse = true;
    [JsonIgnore]
    public bool IsConnected = false;

}


public class PlcAddressInfo
{
    public Guid Id;
    public Guid PlcInfoId;
    public int Index;
    public string Address = "";
    public PlcDataType DataType = PlcDataType.Short16;
    public string Name = "";
    public EncodingType EncodingType = EncodingType.None;
   

    public PlcStateType State = PlcStateType.启用;
    public int Length;
    public string ReMark = "";
    [JsonIgnore]
    public bool ReadSuccess;
    [JsonIgnore]
    public string Value = "";
    public string DefaultValue = "";

}

public enum EncodingType
{
    None,//默认值  即不传的时候
    Default,
    Ansi,
    Gb2312,
    Utf8,
    Ascii,
    Unicode,
    Utf32,
    BigEndianUnicode,
}

public enum PlcType
{
    西门子1200,
    西门子1500,
    西门子300,
    西门子400,
    西门子200Smart,
    基恩士Binary,
    三菱MelsecMcNet,

}


public enum PlcDataType
{
    Bool1,
    Byte8,
    Short16,
    UShort16,
    Int32,
    UInt32,
    Long64,
    ULong64,
    Float单精度,
    Doubles双精度,
    String字符串,
}

public enum PlcStateType
{
    启用,
    停用,
    默认值,
}





